//
//  launchViewController.m
//  HuiWenMovie
//
//  Created by niceDay on 15/7/28.
//  Copyright (c) 2015年 computer. All rights reserved.
//

#import "launchViewController.h"
#import "MainTabBarController.h"
#import "common.h"
#import "UIViewExt.h"

@interface launchViewController ()
{
    NSMutableArray *_imageArray;
    NSInteger index;
    
}
@end

@implementation launchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *image = [UIImage imageNamed:@"Default"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = self.view.bounds;
    [self.view addSubview:imageView];
    [self _creatImageView];
    [self startAnimation];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma  mark - _creatImageView

- (void) _creatImageView{
    NSInteger count = 24;
    CGFloat width = Kwidth/4;
    CGFloat height = Kheight/6;
    CGFloat x = 0;
    CGFloat y = 0;
    _imageArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++) {
        UIImageView  *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        imageView.alpha = 0;
        NSString *imageName = [NSString stringWithFormat:@"%d@2x.png",i+1];
        imageView.image = [UIImage imageNamed:imageName];
        [self.view addSubview:imageView];
        [_imageArray addObject:imageView];
        if (i <= 3) {
            x = i*width;
            y = 0;
        }
        else if (i <= 8){
            x = 3*width;
            y = (i-3)*height;
        }
        else if (i <= 11){
            x = (11-i)*width;
            y = Kheight-height;
        }
        else if (i <= 15){
            x = 0;
            y = (16 - i)*height;
        }
        else if (i <= 17){
            y = height;
            x = (i - 15)*width;
        }
        else if (i <= 20){
            x = 2*width;
            y = (i - 16)*height;
        }
        else if (i <= 23){
            x = width;
            y = (25-i)*height;
        }
        imageView.origin = CGPointMake(x, y);
    
    }
}

- (void)startAnimation{
    if (index >= _imageArray.count) {
       // UIWindow *window = [UIApplication sharedApplication].keyWindow;
        
        UIWindow *window = self.view.window;
        MainTabBarController *mainCtrl = [[MainTabBarController alloc] init];
        mainCtrl.view.transform = CGAffineTransformMakeScale(.2, .2);
        [UIView animateWithDuration:0.1 animations:^{
            mainCtrl.view.transform = CGAffineTransformIdentity;
        }];
        window.rootViewController = mainCtrl;
        return;
    }
    UIImageView *imageView = _imageArray[index];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.2];
    imageView.alpha = 1;
    [UIView commitAnimations];
    index++;
    [self performSelector:@selector(startAnimation) withObject:nil afterDelay:0.2];
}


- (BOOL)prefersStatusBarHidden{
    return YES;
}
@end

